//
//  WalletCashViewController.m
//  HaierGMS_APP
//
//  Created by 堂堂 on 16/12/6.
//  Copyright © 2016年 堂堂. All rights reserved.
//

#import "WalletCashViewController.h"
#import "UIColor+Hex.h"
#import "AFHTTPRequestOperationManager.h"
#import "MBProgressHUD+MJ.h"
#import "HttpReauestParam.h"
#import "UserDefaultUtil.h"
#import "BankCardViewController.h"
@interface WalletCashViewController ()



@end

@implementation WalletCashViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f2f2f2"];
    _money_field.delegate = self;
    [self.limit_btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.limit_btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#ff7e00"]] forState:UIControlStateNormal];
    [self.limit_btn setBackgroundImage:[self createImageWithColor:[UIColor colorWithHexString:@"#55ff7e00"]] forState:UIControlStateSelected];
    
    [self.limit_btn addTarget:self action:@selector(limitClick) forControlEvents:UIControlEventTouchUpInside];
    
    NSString *str = [NSString stringWithFormat:@"<font color=\"#000000\">&nbsp&nbsp&nbsp当前可提现金额<font color=\"#FF3030\">&nbsp%i&nbsp<font color=\"#000000\">元",_amount];
    
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    
    _lab_money.attributedText = attrStr;
    _lab_name.text =[NSString stringWithFormat:@"    %@",[UserDefaultUtil getData:USER_NAME]];
    
}

-(void)limitClick{
    if (_chooseBank == nil) {
        [MBProgressHUD showError:@"请选择银行卡"];
        return;
    }
    if ([_money_field.text isEqualToString:@""]) {
        [MBProgressHUD showError:@"请输入金额"];
        return;
    }
    int money = [_money_field.text integerValue];
    if (money >_amount) {
        [MBProgressHUD showError:@"您的余额不足"];
        return;
    }
    
    NSString *userId =[UserDefaultUtil getUserId];
    NSDictionary * dic = @{@"identifier": userId ,@"id":[NSString stringWithFormat:@"%@",_chooseBank[@"id"]],@"money":_money_field.text};
    
    NSLog(@"%@",dic);
    
    HttpReauestParam * param = [[HttpReauestParam alloc]initWithData:@"wallet_carry_show" content:[self dictionaryToJson:dic] urlType:0];
    AFHTTPRequestOperationManager *manage = [AFHTTPRequestOperationManager manager];
    manage.responseSerializer = [AFHTTPResponseSerializer serializer];
    // [MBProgressHUD showMessage:@"获取订单"];
    [manage POST:param.SEVICE_URL parameters:param.param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * data = [NSJSONSerialization JSONObjectWithData:operation.responseObject options:0 error:nil];
        
        
        if ([data[@"success"] boolValue]) {
           
            [MBProgressHUD showSuccess:@"提交成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [MBProgressHUD showError:data[@"errorMsg"]];
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:@"网络连接失败"];
    }];

}

-(void)viewWillAppear:(BOOL)animated{
    if (_chooseBank != nil) {
        [_bank_image loadImage:[NSURL URLWithString:_chooseBank[@"bankImage"]]];
        _bank_name.text = [NSString stringWithFormat:@"%@(%@)",_chooseBank[@"bankName"],_chooseBank[@"bankCard"]];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if([textField isFirstResponder]){
        [textField  resignFirstResponder];
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (UIImage*) createImageWithColor: (UIColor*) color
{
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage*theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    
    return theImage;
}
-(NSString*)dictionaryToJson:(NSDictionary *)dic

{
    
    NSError *parseError = nil;
    
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&parseError];
    
    return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    
}

- (IBAction)back_click:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)chooseBank_click:(id)sender {
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"App_Main" bundle:[NSBundle mainBundle]];
    BankCardViewController *bankVc = [sb instantiateViewControllerWithIdentifier:@"bank_view"];
    bankVc.isCash = YES;
    [self.navigationController pushViewController:bankVc animated:YES];
}
- (IBAction)touch_view:(id)sender {
     [self.view endEditing:YES];
}
@end
